<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<body>


<script type="text/javascript">

//1. 浅拷贝
var arr1 = ['a','b','c','d'];
var arr2 = arr1;

arr1[0] = '你好吗';
arr2[1] = '还行';

console.log( arr1, arr2 );

var obj1 = {a:1,b:2}
var obj2 = Object.assign(obj1);
obj1.a = '100';
obj2.b = '你怎么养';
console.log( obj1,obj2);


//2. 深拷贝
var obj3 = {
	a:1,
	b:2
}
var obj4 = JSON.parse(JSON.stringify( obj3 ));
obj3.a = '100';
obj4.b = '你怎么阳';
console.log( obj3, obj4 );



var obj5 = {
	a:1,
	b:2,
	arr:['a','b','c','d']
}


function copyObj( obj ){
	if(  Array.isArray(obj)  ){
		var newObj = [];
	}else{
		var newObj = {};
	}
	for( var key in obj ){
		if( typeof obj[key] == 'object' ){
			newObj[key] = copyObj(obj[key]);
		}else{
			newObj[key] = obj[key];
		}
	}
	return newObj;
}
console.log(  copyObj(obj5)  );

</script>
</body>
</html>